package com.ssc.jasperttest;

import java.util.ArrayList;
import java.util.List;

public class Adherent {

	private String nom; 
	private String prenom; 
	private String civilite; 
	private String commentaire; 
	private String adresse1; 
	private String adresse2; 
	private String email1; 
	private String email2; 
	private String ville; 
	private String codePostal; 
	private String fixe1; 
	private String fixe2; 
	private String libFixe1; 
	private String libFixe2; 
	private String port1; 
	private String port2; 
	private String port3;
	private String libPort1;
	private String libPort2;
	private String libEmail1;
	private String libEmail2;
	private String nomEnfant1;
	private String nomEnfant2;
	private String nomEnfant3;
	private String nomEnfant4;
	private String nomEnfant5;
	private String telEnfant1;
	private String telEnfant2;
	private String telEnfant3;
	private String telEnfant4;
	private String telEnfant5;
	
	private List<Adherent> children = new ArrayList<Adherent>();
	private Adherent spouse;
	public Adherent(){
		libFixe1="Fixe";
		libFixe2="Fixe2";
	}
	public void addChild(Adherent adh){
		//children.add(adh);
		if(nomEnfant1==null){
			nomEnfant1 = adh.getPrenom();
			telEnfant1 = adh.getPort1();
		}
		else if(nomEnfant2==null){
			nomEnfant2 = adh.getPrenom();
			telEnfant2 = adh.getPort1();
		}
		else if(nomEnfant3==null){
			nomEnfant3 = adh.getPrenom();
			telEnfant3 = adh.getPort1();
		}
		else if(nomEnfant4==null){
			nomEnfant4 = adh.getPrenom();
			telEnfant4 = adh.getPort1();
		}
		else if(nomEnfant5==null){
			nomEnfant5 = adh.getPrenom();
			telEnfant5 = adh.getPort1();
		}
	}
	/**
	 * 2 adherents sont de même famille s'ils ont même nom de famille et même adresse 
	 */
	public boolean isSameFamillyWith(Adherent adherent){
		if(adherent==null) throw new IllegalArgumentException(" addherent est null");
		if(this.nom==null && adherent.nom==null) return true;
		if(this.nom==null && adherent.nom!=null) return false;
		if(this.nom!=null && adherent.nom==null) return false;
		return this.nom.equals(adherent.nom);
	}
	public boolean isSameAddressWith(Adherent adherent){
		if(adherent==null) throw new IllegalArgumentException(" addherent est null");
		if(onlyOneOfThemNull(this.codePostal, adherent.codePostal)) return false;
		if(onlyOneOfThemNull(this.ville, adherent.ville)) return false;
		if(onlyOneOfThemNull(this.adresse1, adherent.adresse1)) return false;
		if(onlyOneOfThemNull(this.adresse2, adherent.adresse2)) return false;
		
		return nullOrEqual(this.codePostal,adherent.codePostal)
				&&nullOrEqual(this.adresse1,adherent.adresse1)
				&&nullOrEqual(this.ville,adherent.ville)
				&&nullOrEqual(this.adresse2,adherent.adresse2);
	}
	
	boolean onlyOneOfThemNull(Object first, Object second){
		boolean result = false;
		if(first!=null && second==null || first==null && second!=null) result=true;
		return result;
		
	}
	boolean nullOrEqual(String s1, String s2){
		return bothNull(s1, s2) || s1.equals(s2);
	}
	boolean bothNull(Object first, Object second){
		boolean result = false;
		if(first==null && second==null ) result=true;
		return result;
		
	}
	
	public boolean isMarriedWith(Adherent adherent){
		if(adherent==null) throw new IllegalArgumentException(" addherent est null");
		if(this.commentaire==null || this.commentaire.trim().length()==0) return false;
		if(onlyOneOfThemNull(this.commentaire, adherent.commentaire)) return false;
		if(this.isSameFamillyWith(adherent)==false) return false;
		if(!isSameAddressWith(adherent))return false;
		return this.commentaire.equals(adherent.prenom) && adherent.commentaire.equals(this.prenom);
	}
	
	public Adherent getSpouse() {
		return spouse;
	}

	public void setSpouse(Adherent spouse) {
		this.spouse = spouse;
	
	}

	public List<Adherent> getChildren() {
		return children;
	}

	public String getNom() {
		return nom;
	}
	public void setNom(String nom) {
		this.nom = nom;
	}
	public String getLibFixe1() {
		return libFixe1;
	}
	public void setLibFixe1(String libFixe1) {
		this.libFixe1 = libFixe1;
	}
	public String getLibFixe2() {
		return libFixe2;
	}
	public void setLibFixe2(String libFixe2) {
		this.libFixe2 = libFixe2;
	}
	public String getPrenom() {
		return prenom;
	}
	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}
	public String getCivilite() {
		return civilite;
	}
	public void setCivilite(String civilite) {
		this.civilite = civilite;
	}
	public String getCommentaire() {
		return commentaire;
	}
	public void setCommentaire(String commentaire) {
		this.commentaire = commentaire;
	}
	public String getAdresse1() {
		return adresse1;
	}
	public void setAdresse1(String adresse1) {
		this.adresse1 = adresse1;
	}
	public String getAdresse2() {
		return adresse2;
	}
	public String getNomEnfant1() {
		return nomEnfant1;
	}
	public void setNomEnfant1(String nomEnfant1) {
		this.nomEnfant1 = nomEnfant1;
	}
	public String getNomEnfant2() {
		return nomEnfant2;
	}
	public void setNomEnfant2(String nomEnfant2) {
		this.nomEnfant2 = nomEnfant2;
	}
	public String getNomEnfant3() {
		return nomEnfant3;
	}
	public void setNomEnfant3(String nomEnfant3) {
		this.nomEnfant3 = nomEnfant3;
	}
	public String getNomEnfant4() {
		return nomEnfant4;
	}
	public void setNomEnfant4(String nomEnfant4) {
		this.nomEnfant4 = nomEnfant4;
	}
	public String getNomEnfant5() {
		return nomEnfant5;
	}
	public void setNomEnfant5(String nomEnfant5) {
		this.nomEnfant5 = nomEnfant5;
	}
	public String getTelEnfant1() {
		return telEnfant1;
	}
	public void setTelEnfant1(String telEnfant1) {
		this.telEnfant1 = telEnfant1;
	}
	public String getTelEnfant2() {
		return telEnfant2;
	}
	public void setTelEnfant2(String telEnfant2) {
		this.telEnfant2 = telEnfant2;
	}
	public String getTelEnfant3() {
		return telEnfant3;
	}
	public void setTelEnfant3(String telEnfant3) {
		this.telEnfant3 = telEnfant3;
	}
	public String getTelEnfant4() {
		return telEnfant4;
	}
	public void setTelEnfant4(String telEnfant4) {
		this.telEnfant4 = telEnfant4;
	}
	public String getTelEnfant5() {
		return telEnfant5;
	}
	public void setTelEnfant5(String telEnfant5) {
		this.telEnfant5 = telEnfant5;
	}
	public void setAdresse2(String adresse2) {
		this.adresse2 = adresse2;
	}
	public String getEmail1() {
		return email1;
	}
	public void setEmail1(String email1) {
		this.email1 = email1;
	}
	public String getEmail2() {
		return email2;
	}
	public void setEmail2(String email2) {
		this.email2 = email2;
	}
	public String getVille() {
		return ville;
	}
	public String getLibPort1() {
		return libPort1;
	}
	public void setLibPort1(String libPort1) {
		this.libPort1 = libPort1;
	}
	public String getLibPort2() {
		return libPort2;
	}
	public void setLibPort2(String libPort2) {
		this.libPort2 = libPort2;
	}
	public String getLibEmail1() {
		return libEmail1;
	}
	public void setLibEmail1(String libEmail1) {
		this.libEmail1 = libEmail1;
	}
	public String getLibEmail2() {
		return libEmail2;
	}
	public void setLibEmail2(String libEmail2) {
		this.libEmail2 = libEmail2;
	}
	public void setChildren(List<Adherent> children) {
		this.children = children;
	}
	public void setVille(String ville) {
		this.ville = ville;
	}
	public String getCodePostal() {
		return codePostal;
	}
	public void setCodePostal(String codePostal) {
		this.codePostal = codePostal;
	}
	public String getFixe1() {
		return fixe1;
	}
	public void setFixe1(String fixe1) {
		this.fixe1 = fixe1;
	}
	public String getFixe2() {
		return fixe2;
	}
	public void setFixe2(String fixe2) {
		this.fixe2 = fixe2;
	}
	public String getPort1() {
		return port1;
	}
	public void setPort1(String port1) {
		this.port1 = port1;
	}
	public String getPort2() {
		return port2;
	}
	public void setPort2(String port2) {
		this.port2 = port2;
	}
	public String getPort3() {
		return port3;
	}
	public void setPort3(String port3) {
		this.port3 = port3;
	}

	public boolean isMale() {
		if(civilite==null||civilite.trim().equals("")) throw new IllegalStateException("civilite indéfinie !");
		
		return "fr".equalsIgnoreCase(civilite);
	}
	@SuppressWarnings("null")
	public String getPrenomAbrege() {
		StringBuffer result = new StringBuffer();
		Adherent spouse=getSpouse();
		if(spouse==null)return getPrenom();
		String prenonmEpoux = spouse.getPrenom();
		String monPrenom = this.getPrenom();
		if(monPrenom!=null && prenonmEpoux!=null){
			//String reference = plusLong(monPrenom,prenonmEpoux);
//			String autre= null;
//			if(reference.equals(monPrenom)){
//				autre =prenonmEpoux;
//			}
//			else autre=monPrenom;
			for(int i=0;i<Math.min(3, monPrenom.length());i++){
				result.append(monPrenom.substring(i,i+1)) ;
				if(!monPrenom.substring(i,i+1).equals(prenonmEpoux.substring(i,i+1))){
					break;
				}
			//	else result.
			}
			
		}
		return result.toString();
	} 
	
	private String plusLong(String s1, String s2){
		int max=	Math.max(s1.length(), s2.length());
		if(max==s1.length()) return s1;
		else return s2;
	}
	
	
}
